import 'package:chathouse/pages/client/home/service_detail.dart';
import 'package:chathouse/pages/route_manager/root_route_manager.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/util/log.dart';
import 'package:chathouse/widgets/base_appbar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../constant/app_constant.dart';
import '../../../model/client_search_model.dart';

/// @class : ServiceAndRepairPage
/// @date : 2023-02-27 17:33
/// @name : LFC
/// @description :服務與維修工程
class ServiceAndRepairPage extends StatefulWidget {
  const ServiceAndRepairPage({Key? key}) : super(key: key);

  @override
  State<ServiceAndRepairPage> createState() => _ServiceAndRepairPageState();
}

class _ServiceAndRepairPageState extends State<ServiceAndRepairPage>
    with NavigationHelper {
  ClientSearchModel? serviceModel;
  ClientSearchModel? engineeringModel;

  @override
  void initState() {
    super.initState();
    Obj.showLoading();
    request();
  }

  void request() {
    Apis.getServiceList().then((response) {
      Obj.dismissLoading();
      var json = response?.data;
      if (json == null) {
        return;
      }
      setState(() {
        serviceModel = ClientSearchModel.fromJson(json);
      });
    }).catchError((error) {
      LogUtil.d("getServiceList error $error");
      Obj.dismissLoading();
    });

    Apis.getEngineeringList().then((response) {
      Obj.dismissLoading();
      var json = response?.data;
      if (json == null) {
        return;
      }
      setState(() {
        engineeringModel = ClientSearchModel.fromJson(json);
      });
    }).catchError((error) {
      Obj.dismissLoading();
      LogUtil.d("getEngineeringList error $error");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: getAppBar(
        title: "服務/維修",
        titleTextStyle: getTextStyle(40.sp,
            color: MyColors.green_79141891, fontType: FontType.BOLD),
        leading: Icon(
          Icons.arrow_back_ios_rounded,
          size: 26.w,
          color: MyColors.gary_149149149,
        ).setOnClickListener(() {
          FocusManager.instance.primaryFocus?.unfocus();
          back();
          // Get.back();
        }),
      ),
      body: CustomScrollView(
        slivers: [
          SliverToBoxAdapter(
            child: Container(
              height: 0.5.w,
              color: MyColors.gary_165165165,
            ),
          ),
          SliverToBoxAdapter(
            child: Text("服務",
                    style: getTextStyle(30.sp,
                        color: MyColors.green_69157608,
                        fontType: FontType.BOLD))
                .setContainer(
                    alignment: Alignment.center,
                    margin: EdgeInsets.symmetric(vertical: 12.w)),
          ),
          SliverPadding(
            padding: EdgeInsets.symmetric(horizontal: 28.w),
            sliver: SliverGrid(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext ctx, int index) {
                    return Column(
                      children: [
                        AspectRatio(
                          aspectRatio: 162 / 108,
                          child: ImageUtils.image(
                              serviceModel?.data?[index].thumbnailImg ?? "",
                              imageType: ImageType.NETWORK,
                              fit: BoxFit.fill,
                              radius: 20.w),
                        ),
                        Text(serviceModel?.data?[index].serviceName ?? "",
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            textAlign: TextAlign.center,
                            style: getTextStyle(
                              18.sp,
                              fontType: FontType.BOLD,
                              color: MyColors.black_0000011,
                            )),
                      ],
                    ).setOnClickListener(() {
                      // Get.to(ServiceDetailPage(
                      //     serviceModel?.data?[index].serviceId ??
                      //         AppConstant.testWaterService));
                      toServeDetailPage(
                          serviceModel?.data?[index].serviceId ??
                              AppConstant.testWaterService);
                    }, isInkWell: true);
                  },
                  childCount: serviceModel?.data?.length ?? 0,
                ),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 1,
                  mainAxisSpacing: 0,
                  crossAxisSpacing: 24.w,
                )),
          ),
          SliverToBoxAdapter(
            child: Text("維修及工程",
                    style: getTextStyle(30.sp,
                        color: MyColors.green_69157608,
                        fontType: FontType.BOLD))
                .setContainer(
                    alignment: Alignment.center,
                    margin: EdgeInsets.symmetric(vertical: 12.w)),
          ),
          SliverPadding(
            padding: EdgeInsets.symmetric(horizontal: 28.w),
            sliver: SliverGrid(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext ctx, int index) {
                    return Column(
                      children: [
                        AspectRatio(
                          aspectRatio: 162 / 108,
                          child: ImageUtils.image(
                              engineeringModel?.data?[index].thumbnailImg ?? "",
                              imageType: ImageType.NETWORK,
                              fit: BoxFit.fill,
                              radius: 20.w),
                        ),
                        Text(engineeringModel?.data?[index].serviceName ?? "",
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            textAlign: TextAlign.center,
                            style: getTextStyle(
                              18.sp,
                              fontType: FontType.BOLD,
                              color: MyColors.black_0000011,
                            )),
                      ],
                    ).setOnClickListener(() {
                      // Get.to(ServiceDetailPage(
                      // engineeringModel?.data?[index].serviceId ??
                      //     AppConstant.testWaterService));
                      toServeDetailPage(
                          engineeringModel?.data?[index].serviceId ??
                              AppConstant.testWaterService);
                    }, isInkWell: true);
                  },
                  childCount: engineeringModel?.data?.length ?? 0,
                ),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 1,
                  mainAxisSpacing: 0,
                  crossAxisSpacing: 24.w,
                )),
          ),
        ],
      ),
    );
  }

  void toServeDetailPage(num id) {
    LogUtil.d("toServeDetailPagetoServeDetailPage");
    navigateTo(ServiceDetailPage(id));
  }
}
